<?php
class Action_Community_VoteOps extends RL_Handler
{

	private function determineAction() {
		if (isset($this->rh->POST['vote_variant']))
			return 1;
		if (isset($this->rh->POST['vote_close_button']))
			return 2;
		if (isset($this->rh->POST['vote_open_button']))
			return 3;

		return 0;
	}

	public function process ()
	{
		$aa = $this->module->aa;
		$aa->getInfo();

		// проверить права на то, что у этого пользователя в этом сообществе есть право на комментироввание записи
		// на которой он голосует

		$this->rh->UseClass("Model_Item");
		$vm = $this->ctx->mm->getModel("Vote", "m");

		$act = $this->determineAction();

		switch($act) {
			case 1:
			if ($vm->checkUserVote($this->params['id_item'], $aa->acl_data['user_id'])) {
				$message = "user is already voted";
				break;
			}

			// проверить а нет ли голоса от этого пользователя?
			$this->ctx->db->begin();
			$st = $vm->addUserVote($this->params['id_item'], $this->rh->POST['vote_variant'], $aa->acl_data['user_id']);
			if ($st) {
				// запостить комментарий
				$cm = $this->ctx->mm->getModel("Comment", "m");
				$vote = $vm->getItem( $this->params['id_item'] );

				$fcomment = $cm->submitComment( 'Проголосовал за вариант "'.$vote->data['answers'][ $this->rh->POST['vote_variant'] ]['text'].'"', $this->params['id_item'], $aa->acl_data['user_id'], 1 );
				if ($fcomment) {
					$aa->createComment( $fcomment->id, $this->params['id_item'] );
				}

				if (!$this->ctx->db->isErrorInTransaction()) $message = "OK";
					else {
						$message = "DB Transaction rollback";
						break;
					}
			}
			$this->ctx->db->end();

			break;

			case 2:
			$this->ctx->db->begin();
			$fvote = $vm->getItem($this->params['id_item']);

			if ($fvote->data['posted_by_id'] != $aa->acl_data['user_id']) {
				$message = "You can't close this vote.";
				break;
			}

			$vm->closeVote($this->params['id_item']);
			if (!$this->ctx->db->isErrorInTransaction()) $message = "Vote closed";
				else {
					$message = "DB Transaction rollback";
					break;
				}

			$this->ctx->db->end();
			break;

			case 3:
			$this->ctx->db->begin();
			$fvote = $vm->getItem($this->params['id_item']);

			if ($fvote->data['posted_by_id'] != $aa->acl_data['user_id']) {
				$message = "You can't open this vote.";
				break;
			}

			$vm->openVote($this->params['id_item']);
			if (!$this->ctx->db->isErrorInTransaction()) $message = "Vote closed";
				else {
					$message = "DB Transaction rollback";
					break;
				}

			$this->ctx->db->end();
			break;

			case 0:
			return false; // 404
			break;
		}

		//$view = $this->rh->views->findByName('msg');
		//$view->data['message'] = $message;

		//$this->rh->response->setContent( $view );
		//if (isset($new_location)) {
		//	$this->rh->redirect($new_location, 1);
		//}

		// для проголосовавших редирект на страницу итема
		$this->rh->UseClass('RL_URLGenerator');
		$ug = new RL_URLGenerator($this->rh->router->getRule('showitem'));
		$ug->setRouter($this->rh->router);
		$ug->setParams(array(
			'community_url' => $this->params['community_url'],
			'id_item' => $this->params['id_item'],
		));
		$url = $ug->generateUrl();
		if ($url) {
			$this->rh->redirect($url);
		} else {
			$this->rh->response->setContent( "Wazzup?!" );
		}
	} // end of function process

}
